package com.lxy.demo.servlet;

import com.lxy.demo.service.UserService;
import com.lxy.demo.service.UserServiceImpl;

import javax.servlet.*;
import javax.servlet.http.HttpServlet;
import java.io.IOException;

public class MyServlet3 extends HttpServlet {

    //创建业务层对象
    static UserService userService = new UserServiceImpl();

    String encoding = null;

    @Override
    public void init(ServletConfig config) throws ServletException {
        encoding =  config.getInitParameter("encoding");
    }

    @Override
    public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
        req.setCharacterEncoding(encoding);
        res.setContentType("text/html;charset=utf-8");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        try {
            boolean user = userService.findUser(username, password);
            if (user){
                res.getWriter().write("<script>alert('登录成功!')</script>");
            }else {
                res.getWriter().write("<script>alert('登录失败!')</script>");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void destroy() {
        super.destroy();
    }

}
